package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "c 端用户添加购物车功能")
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**端用户添加购物车功能
     *
     * @param shoppingCartDTO
     * @return
     */

    @ApiOperation("用户添加购物车功能")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("用户添加购物车商品信息：{}",shoppingCartDTO);

        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }


    /**查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车")
    public Result<List<ShoppingCart>> getShoppingCart(){

        log.info("查询购物车");
        List<ShoppingCart> list = shoppingCartService.getShoppingCart();
        return Result.success(list);

    }

    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result cleanShoppingCart(){
        log.info("清空购物车");
        shoppingCartService.cleanShoppingCart();
        return Result.success();

    }


    /**更新购物车中的一个商品的数量
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("更新购物车中的一个商品的数量")
    public Result subOne(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("更新购物车中的一个商品的数量，{}",shoppingCartDTO);
        shoppingCartService.subOne(shoppingCartDTO);

        return Result.success();
    }
}
